//: [Previous](@previous)

import Foundation

//翻转数字
func reverse(num: Int) -> Int {
    var temp = num
    var new = 0
    while temp != 0 {
        //对数字取余,取出最末位数字
        let lastNum = temp%10
        //对数字除以10,舍弃掉最末位数字
        temp /= 10
        new = new * 10 + lastNum
    }
    return new
}
reverse(num: 61234)




//使用递归,计算0-100的和
func sum(number: Int) -> Int {
    if number == 0 {
        return 0
    }else {
        return number + sum(number: number - 1)
    }
}
sum(number: 100)


//冒泡排序
func maopaoSort(array: inout Array<Int>) -> Array<Int> {
    
    //遍历
    for i in 0..<array.count {
        //遍历后续的元素,排序
        for j in (i + 1)..<array.count {
            //当前元素
            let current = array[i]
            //下一个元素
            let next = array[j]
            //如果
            if next < current {
                array[i] = next
                array[j] = current
            }
        }
        print(array)
    }
    return array
}

var testArr = [3,15,6,7,1,8,9,4]
maopaoSort(array: &testArr)

//选择排序
func chooseSort(array: inout Array<Int>) -> Array<Int> {
    for i in 0..<array.count {
        //记录当前最小值索引
        var minIdx = i
        for j in i+1..<array.count {
            if array[minIdx] > array[j] {
                //更新最小值
                minIdx = j
            }
        }
        //交换i和实际最小值的索引
        array.swapAt(minIdx, i)
    }
    return array
}
